import { createRouter,createWebHistory } from 'vue-router'
import LayOut from '../views/Layout/LayOut.vue'
// import Abc from '../views/Abc.vue'
import store from "../store/index.js"
import axios from 'axios';

const routes = [
  {
    path:"/",
    name:"login",
    component:()=>import("../views/pages/login.vue")
  },
  {
    path:"/bigData",
    name:"bigData",
    component:()=>import("../views/dataShow/bigData.vue")
  },
  {
    path:"/:catchAll(.*)",
    name:"404",
    component:()=>import("../views/except/404.vue")
  },
  {
    path:"/lineChart",
    name:"lineChart",
    component:()=>import("../views/dataShow/components/lineChart.vue")
  },
  {
    path:"/main",
    name:"数据分析",
    component: LayOut,
    redirect:"/index",
    icon: 'iconfont icon-shujufenxi',
    show1: true,
    show2: false,
    children:[
      {
        path:"/index",
        name:"首页",
        component:()=>import("../views/pages/index.vue")
      },
      {
        path:"/bigData",
        name:"数据大屏",
        component:()=>import("../views/dataShow/bigData.vue")
      }
    ]
  },
  {
    path:"/main",
    name:"用户管理",
    component: LayOut,
    redirect:"/index",
    show1: true,
    show2: false,
    icon: 'iconfont icon-yonghuguanli',
    children:[
      {
        path:"/user",
        name:"用户信息",
        component:()=>import("../views/pages/usersList.vue")
      }, {
        path:"/wx_user",
        name:"消费者信息",
        component:()=>import("../views/admin/wx_user.vue")
      }
    ]
  },{
    path:"/main",
    name:"菜单管理",
    component: LayOut,
    show1: true,
    show2: false,
    icon: 'iconfont icon-yinliao',
    children:[
      {
        path:"/category",
        name:"分类管理",
        component:()=>import("../views/admin/category.vue")
      },
      
      {
        path:"/goods",
        name:"餐品管理",
        component:()=>import("../views/admin/goods.vue")
      }
    ]
  },{
    path:"/main",
    name:"成本管理",
    component: LayOut,
    show1: true,
    show2: false,
    icon: 'iconfont icon-chengbenjilu',
    children:[
      {
        path:"/cost_type",
        name:"账单分类",
        component:()=>import("../views/admin/cost_type.vue")
      },   
     {
        path:"/cost",
        name:"账单详情",
        component:()=>import("../views/admin/cost.vue")
      }
    ]
  },{
    path:"/main",
    name:"订单管理",
    component: LayOut,
    show1: true,
    show2: true,
    icon: 'iconfont icon-icon-order',
    children:[
      {
        path:"/orders",
        name:"订单信息",
        component:()=>import("../views/admin/orders.vue")
      },{
          path:"/buyed_goods",
          name:"已售商品信息",
          component:()=>import("../views/admin/buyed_goods.vue")
        }
        ,{
            path:"/addresses",
            name:"收货地址",
            component:()=>import("../views/admin/addresses.vue")
      }
    ]
  },{
    path:"/main",
    name:"业务运营",
    component: LayOut,
    icon: 'iconfont icon-yunyingliucheng',
    show1: true,
    show2: true,
    children:[
      {
        path:"/finish_order",
        name:"制作餐品",
        component:()=>import("../views/employee/finish_order.vue")
      },{
        path:"/buyed_goods",
        name:"外卖状态",
        component:()=>import("../views/admin/buyed_goods.vue")
      }
    ]
  }
]


// //动态获取路由

// //格式化返回的路由信息
// const formatRoutes = routes => {
//   let resultRoutes = [];
//   routes.forEach(item => {
//     //如果有孩子，继续对孩子格式化
//     item.children = !!item.children ?
//       formatRoutes(item.children) :
//       null; 
//     let resRoutes = {
//       path: item.path,
//       component: resolve => {
//         require([
//           `../components/mgt/${item.component}.vue`
//         ], resolve);
//       },
//       name: item.name,
//       nameZh: item.nameZh,
//       iconCls: item.iconCls,
//       meta: {
//         requireAuth: true,
//         title: item.nameZh
//       },
//       children: item.children
//     };
//     resultRoutes.push(resRoutes);
//   });
//   return resultRoutes;
// };

// //初始化菜单 采用箭头函数来实现
// const initMenuList = (router, store) => {
//   if (store.state.menuList.length > 0) {
//     return;
//   } else {
//     //  menuListApi.getMenuList()
//     axios.get(`/user/getMenu/admin`)
//       .then(res => {
//         if (res && res.status == 200) {
//           let result = formatRoutes(res.data);
//           console.log("后台的路由"+res.data)
//           router.addRoutes(result);
//           // router.options.routes = router.options.routes.concat(
//           //   result
//           // );
//           //如果菜单中有数据才去添加到store中,否则为空数组
//           result.length > 0 ? store.dispatch("initMenuLists", result) : [];
//         }
//       })
//       .catch(error => {
//         console.log(error);
//       });
//   }
// };

// const router = new VueRouter({
//   mode: "history",
//   base: process.env.BASE_URL,
//   routes
// });

const router = createRouter({
  history: createWebHistory(),
  routes
})


// //路由跳转判断vuex有菜单数据，没有则取获取菜单数据，再动态渲染，否则不去获取。
// router.beforeEach((to, from, next) => {
//   if (store.state.username && to.path.startsWith("/mgt")) {
//     initMenuList(router, store)
//     // console.log(to)
//   }
//   //将to的title赋值给document
//   if (to.meta.title) {
//     document.title = to.meta.title
//   }
//   //如果刷新了，重新渲染菜单
//   if (store.state.menuList.length === 0) {
//     console.log(to.fullPath)
//     //如果是去登录页，则不再初始化菜单
//     if (to.fullPath == '/login') {

//     } else {
//       initMenuList(router, store)
//     }
//   } else {
//     next()
//   }
//   next()
// });



// router.beforeEach((to,form,next)=>{
// console.log("store",store.state.uInfo)
// const uInfo=store.state.uInfo.userInfo
// if(!uInfo.username){
//   if(to.path=="/login"){
//     next()
//     return
//   }
//   next("/login")
// }else{
//   next()
// }

// })

export default router
